#!/usr/bin/ruby

QS_PATH = "questions.dat"
EXAM_PATH = "exam.txt"

qs = nil
File.open(QS_PATH,"r") do |f|
  qs = Marshal.load(f.read)
end

raise "ERR : qs is nil" unless qs

EXAM_HEAD =<<DOC_HEAD
  Exam Name (#{Time.now})
  姓名:          分数:


DOC_HEAD

exam_txt = ""
srand
20.times do |i|
  idx = (rand * qs.count).to_i
  exam_txt << "#{i.to_s}" + ". " + qs[idx][1] + "\n" + qs[idx][2] + \
  "\n选择:[#{idx}]:(   )\n\n"
end

File.open(EXAM_PATH,"w") do |f|
  f.write EXAM_HEAD
  f.write exam_txt
end
